﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;

namespace GRWL.BZSC.UI
{
    public partial class EditDepotFrm : BaseFrm
    {
        //public bool CallDone { get; set; }
        private TB_Depot Depot { get; set; }
        private TB_User User;

        /// <summary>
        ///新增时当前所在分类
        /// </summary>
        //public SelectItem CurrentCategory { get; set; }
        public int Type { get; set; }

        /// <summary>
        /// 传入编码规则
        /// </summary>
        //public CodeRuleFrm.CodeRule codeRule { get; set; }

        /// <summary>
        /// 编辑模式时传入id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 新增调用设定为 true, 编辑调用设定为 false
        /// </summary>
        public bool EditMode { get; set; }

        //public event GRWL.BZSC.Common.EventCommon.UpdateDataGridViewDlgt UpdateDataGridViewEvent;

        public EditDepotFrm()
        {
            InitializeComponent();

            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();
        }

        private void EditDepotFrm_Load(object sender, EventArgs e)
        {
            //新增记录时此值为空
            //if (Depot == null)
            if (!EditMode)
            {
                Depot = new TB_Depot();
                //typeUTextBox.Text = Type.ToString();
                //指定新记录中的默认分类
                Depot.type = Type;
                return;
            }

            Depot = this.BaseDataProxy.GetDepot(Id);
            if (Depot.storekeeper != null)
                User = BaseDataProxy.GetUser((int)Depot.storekeeper);
            //编辑记录的内容绑定到对应控件
            //idUTextBox.Text = Depot.id.ToString();
            nameUTextBox.Text = Depot.name;
            codeUTextBox.Text = Depot.code;
            if (User != null)
                edtStorekeeper.Text = User.name; //Depot.storekeeper.ToString();
            mrpFlagCheckBox.Checked = Depot.mrpFlag;
            watingFlagCheckBox.Checked = Depot.watingFlag;
            badFlagCheckBox.Checked = Depot.badFlag;
            scrapFlagCheckBox.Checked = Depot.scrapFlag;
        }

        private int? GetStoreKeeperId()
        {
            int? ReturnId;
            try
            {
                ReturnId = Convert.ToInt16(edtStorekeeper.Text);
            }
            catch
            {
                ReturnId = null;
            }
            return ReturnId;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //种子字段,不需要赋值
            //Depot.id = Convert.ToInt16(idUTextBox.Text, null);
            Depot.name = nameUTextBox.Text;
            Depot.code = codeUTextBox.Text;
            //Depot.storekeeper = GetStoreKeeperId();
            Depot.mrpFlag = mrpFlagCheckBox.Checked;
            Depot.watingFlag = watingFlagCheckBox.Checked;
            Depot.badFlag = badFlagCheckBox.Checked;
            Depot.scrapFlag = scrapFlagCheckBox.Checked;

            GRWL.BZSC.UI.Common.OperateType opType = Common.OperateType.Add;
            try
            {
                if (Id==0) //(String.IsNullOrEmpty(idUTextBox.Text))
                {
                    int g = BaseDataProxy.AddDepot(Depot);
                    if (g > 0)
                    {
                        Depot.id = g;
                        MessageBox.Show("已成功保存记录.");
                    }
                }
                else
                {
                    opType = Common.OperateType.Update;
                    if (BaseDataProxy.UpdateDepot(Depot))
                    {
                        MessageBox.Show("已成功保存记录.");
                    }
                }
            }
            catch (InvalidCastException eMsg)
            {
                MessageBox.Show(eMsg.Message.ToString());
            }
            CommonEventArg<TB_Depot> arg = new CommonEventArg<TB_Depot>(Depot, opType);
            GRWL.BZSC.Common.EventCommon.OnDepotUdDGVEvnt(sender, arg);
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSelStoreKeeper_Click(object sender, EventArgs e)
        {
            UserFrm frm = new UserFrm();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                edtStorekeeper.Text = frm.SelectorItem.Name;
                Depot.storekeeper = Common.StringToInt(frm.SelectorItem.Value);
            }
        }

    }
}
